<?php
namespace App\Http\Controllers\Api\Buz;

use App\Http\Controllers\Controller;
use App\Lib\Code;
use App\Models\PlatUserBank;
use App\Repositories\Criteria\AuthUserCriteria;
use App\Repositories\Criteria\OrderStatusCriteria;
use App\Repositories\Criteria\PmSelectCriteria;
use App\Repositories\Eloquent\PlatUserBankRepositoryEloquent;
use App\Services\ApiResponseService;
use App\Validators\Buz\RemitBankValidator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Prettus\Validator\Exceptions\ValidatorException;

class RemitController extends Controller
{
    protected $bankRepository;

    public function __construct(PlatUserBankRepositoryEloquent $platUserBankRepositoryEloquent)
    {
        $this->bankRepository = $platUserBankRepositoryEloquent;
        $this->bankRepository->pushCriteria(AuthUserCriteria::class);
    }

    public function update(Request $request, RemitBankValidator $bankValidator, $id)
    {
        $data = $request->all(array_keys($bankValidator->getRules('update')));
        $bankValidator->with($data)->passesOrFail('update');
        $bank = $this->bankRepository->find($id);
        $bank->update($data);
        return ApiResponseService::success(Code::SUCCESS);
    }

    public function store(Request $request, RemitBankValidator $bankValidator)
    {
        $data = $request->all(array_keys($bankValidator->getRules('create')));
        $bankValidator->with($data)->passesOrFail('create');
        $data['uid'] = Auth::id();
        $this->bankRepository->popCriteria(AuthUserCriteria::class);
        $this->bankRepository->create($data);
        return ApiResponseService::success(Code::SUCCESS);
    }

    public function index(Request $request)
    {
        $billtype = $request->input('billType', '-');
        if ($billtype !== '-') {
            $this->bankRepository->pushCriteria(new PmSelectCriteria($billtype, 'category'));
        }
        $bank = $this->bankRepository
            ->with('city')
            ->paginate(15, ['id', 'classify', 'username', 'category', 'account', 'city_id', 'branch', 'number', 'created_at', 'is_default']);
        return ApiResponseService::returnData($bank);
    }

    public function detail($id)
    {
        $bank = $this->bankRepository->with(['city' => function ($query) {
            $query->select('id', 'parent', 'name');
        }])->find($id, ['id', 'classify', 'username', 'category', 'account', 'city_id', 'branch', 'number']);
        return ApiResponseService::returnData($bank);
    }

    public function delete($id)
    {
        $bank = $this->bankRepository->find($id);
        if ($bank) {
            if ($this->bankRepository->all()->count() <= 1) {
                return ApiResponseService::showError(Code::FATAL_ERROR, '不能删除所有银行卡');
            }
            if ($bank->is_default == 1) {
                $firstBank = PlatUserBank::where('uid', Auth::id())->where('id', '<>', $id)->first();
                $firstBank->is_default = 1;
                $firstBank->save();
            }
            $this->bankRepository->delete($id);
            return ApiResponseService::success();
        }
        return ApiResponseService::showError(Code::NOTFOUND);

    }

    public function setDefault($id)
    {
        $bank = $this->bankRepository->find($id);
        if ($bank->is_default == 0) {
            $bank->is_default = 1;
            $bank->save();
            PlatUserBank::where('id', '<>', $id)->where('is_default', 1)->where('uid', Auth::id())->update(['is_default' => 0]);
        }
        return ApiResponseService::success();
    }
}
